<?php

namespace app\admin\controller;

use app\common\controller\Backend;

// 系统配置的控制器

class Admin extends Backend{
    public function __construct()
    {
        parent::__construct();

        $this->AdminModel=model('Admin');
    }

    // 管理员列表
    public function admin(){
        $count=$this->AdminModel->count();

        $adminlist=$this->AdminModel->paginate(10);

        $pagestr=$adminlist->render();

        $this->assign([
            'count'=>$count,
            'adminlist'=>$adminlist,
            'pagestr'=>$pagestr,
        ]);

        return $this->fetch();
    }

    // 管理员编辑
    public function edit(){
        // 获取管理员的id
        $adminid=$this->request->param('adminid',0);

        $row=$this->AdminModel->where(['id'=>$adminid])->find();

        if(!$row){
            $this->error('该管理员不存在');
            exit;
        }

        if($this->request->isPost()){
            $username=$this->request->param('username','');
            $nickname=$this->request->param('nickname','');
            $oldpass=$this->request->param('oldpass','');
            $password=$this->request->param('password','');
            $repass=$this->request->param('repass','');
            $state=$this->request->param('state','');

            $oldsalt=$row['salt'];
            $oldpass=md5($oldpass.$oldsalt);
            
            if($oldpass != $row['password']){
                $this->error('旧密码输入错误，请重新输入');
                exit;
            }

            if($password != $repass){
                $this->error('确认密码和新密码不一致');
                exit;
            }

            $salt=randstr(10);
            $password=md5($password.$salt);

            $data=[
                'id'=>$adminid,
                'username'=>$username,
                'nickname'=>$nickname,
                'password'=>$password,
                'salt'=>$salt,
                'state'=>$state,
            ];

            // 更新数据
            $result=$this->AdminModel->isUpdate(true)->save($data);

            if($result === FALSE){
                $this->error($this->AdminModel->getError());
                exit;
            }else{
                $this->success('修改成功',url('admin/admin/admin'));
                exit;
            }

        }

        $this->assign([
            'row'=>$row,
        ]);

        return $this->fetch();
    }

    // 添加管理员
    public function add(){
        if($this->request->isPost()){
            $username=$this->request->param('username','');
            $nickname=$this->request->param('nickname','');
            $password=$this->request->param('password','');
            $repass=$this->request->param('repass','');

            $admin=$this->AdminModel->where(['username'=>$username])->find();

            if($admin){
                $this->error('该管理员已存在');
                exit;
            }

            if($password != $repass){
                $this->error('密码和确认密码不一致');
                exit;
            }

            $salt=randstr(10);
            $password=md5($password.$salt);

            $data=[
                'username'=>$username,
                'nickname'=>$nickname,
                'password'=>$password,
                'salt'=>$salt,
            ];

            $result=$this->AdminModel->validate('common/Admin')->save($data);

            if($result === FALSE){
                $this->error($this->AdminModel->getError());
                exit;
            }else{
                $this->success('添加管理员成功',url('admin/admin/admin'));
                exit;
            }
        }
        return $this->fetch();
    }

    // 删除管理员
      // 删除
      public function del()
      {
          $success = [
              'result' => false,
              'msg' => ''
          ];
  
          if($this->request->isAjax())
          {
              $ids = $this->request->param('ids',0);
  
              $result = $this->AdminModel->destroy($ids);
  
              if($result === FALSE){
                  $success['result'] = false;
                  $success['msg'] = $this->AdminModel->getError();
              }else
              {
                  $success['result'] = true;
                  $success['msg'] = '删除管理员成功';
              }
              return json($success);
          }
      }
}